package com.wensi.helper

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.core.app.NotificationCompat
import com.wensi.utils.L

/**
 * 通知栏的帮助类
 */
object NotificationHelper {

    private lateinit var context: Context
    private lateinit var nm: NotificationManager

    private const val CHANNEL_NAME = "语音服务"
    private const val CHANNEL_ID = "voice_service"

    fun initNotifiHelper(context: Context) {
        this.context = context

        nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        // 8.0以上 创建渠道
        setBindVoiceChannel()
    }

    /**
     * 设置绑定服务的渠道
     */
    private fun setBindVoiceChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
            // 呼吸灯
            channel.enableLights(false)
            // 震动
            channel.enableVibration(false)
            // 角标
            channel.setShowBadge(false)

            nm.createNotificationChannel(channel)
        }
    }
    // 绑定语音服务
    fun bindVoiceService(desc: String): Notification {
        // 创建通知栏对象
        val notificationCompat = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationCompat.Builder(context, CHANNEL_ID)
        } else {
            NotificationCompat.Builder(context)
        }
        // 设置白标题
        notificationCompat.setContentTitle(CHANNEL_NAME)
        // 设置描述
        notificationCompat.setContentText(desc)
        // 设置时间
        notificationCompat.setWhen(System.currentTimeMillis())
        // 设置禁止滑动-关闭
        notificationCompat.setAutoCancel(false)
        L.i("服务绑定成功~")
        return notificationCompat.build()
    }
}